<template>
    <div class="bg-white px-3" style="margin: -20px; margin-top:-1rem; margin-bottom: 0!important;">
        <el-tabs v-model="tabIndex" @tab-click="getList" class="ml-1">
            <el-tab-pane v-for="(tab, tabI) in tabBars" :key="tabI" :label="tab.name">

            </el-tab-pane>
        </el-tabs>


        <!-- 标签页的内容 -->
        <!-- 高级搜索-->
        <button-search ref="buttonSearch" placeholder="要搜索的商品名称" @search="searchEvent">
            <!-- 会匹配和使用到slot name=left的插槽 -->
            <template #left>
                <!-- 左边的插槽 -->
                <router-link :to="{ path: '/shop/goods/create' }">
                    <el-button type="success" size="mini" style="margin-right: 3px;">发布商品</el-button>
                </router-link>
                <el-button type="warning" size="mini" v-if="tab === 'delete'"
                    @click="doEvent('restore')">恢复商品</el-button>
                <el-button type="danger" size="mini" v-if="tab === 'delete'"
                    @click="doEvent('destory')">彻底删除</el-button>
                <el-button type="danger" size="mini" v-if="tab !== 'delete'" @click="deleteALL">批量删除</el-button>
                <el-button type="success" size="mini" v-if="tab === 'all' || tab === 'off'"
                    @click="changeStatus(1)">上架</el-button>

                <el-button type="danger" size="mini" v-if="tab === 'all' || tab === 'saling'"
                    @click="changeStatus(0)">下架</el-button>
            </template>
            <!-- 高级搜索的表单 -->
            <template #form>
                <!-- 表单 -->
                <el-form inline ref="form" :model="form" label-width="70px">
                    <el-form-item label="商品名称" class="mb-0">
                        <el-input v-model="form.title" size="mini" placeholder="输入商品名称">

                        </el-input>
                    </el-form-item>
                    <el-form-item label="商品分类" class="mb-0">

                        <el-select v-model="form.category_id" placeholder="请选择" size="mini">
                            <el-option v-for="item in cateOptions" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="info" size="mini" @click="searchEvent">搜索</el-button>
                        <el-button size="mini" @click="clearSearch">清空筛选条件</el-button>
                    </el-form-item>
                </el-form>
            </template>
        </button-search>
        <!-- 商品列表 -->
        <el-table :data="tableData" style="border: 1px; margin-top: 20px; background-color: antiquewhite;"
            @selection-change="handleSelectionChange">
            <!-- 添加顶部间距 -->
            <el-table-column type="selection" width="45" style="align-items: center;" />
            <el-table-column label="商品" width="300px">
                <template slot-scope="scope">
                    <div class="media">
                        <img :src="scope.row.cover" style="width:60px; height:60px" class="mr-3" alt="...">
                        <div class="media-body">
                            <h6 class="mt-0"> {{ scope.row.title }}</h6>
                            <p class="mb-0">分类： {{ scope.row.category.name }}</p>
                            <p class="mb-1">时间： {{ scope.row.create_time }}</p>
                        </div>
                    </div>
                    <!-- {{ scope.row.title }
                    {{ scope.row.cover }}
                    {{ scope.row.create_time }}
                    {{ scope.row.category }} -->
                </template>
            </el-table-column>
            <el-table-column prop="sale_count" label="实际销量" align="center" />
            <el-table-column label="商品排序" align="center">
                <template slot-scope="scope">
                    <span class="badge" :class="scope.row.status ? 'badge-success' : 'badge-danger'">{{ scope.row.status
                        ? '上架' : '仓库' }}</span>
                    <!-- <el-button :type="scope.row.status ? 'success' : 'danger'" size="mini" plain
                        @click="changeStatus(scope.row)">{{ scope.row.status ? '上架' : '下架' }}</el-button> -->
                </template>
            </el-table-column>
            <el-table-column prop="status" label="审核状态" align="center">
                <template slot-scope="scope">
                    <div class="d-flex flex-column" v-if="!scope.row.ischeck">
                        <el-button type="success" size="mini" plain @click="checkGoods(scope.row, 1)">审核通过</el-button>
                        <el-button class="ml-0 mt-1" type="danger" size="mini" plain
                            @click="checkGoods(scope.row, 2)">审核拒绝</el-button>
                    </div>
                    <span v-else>{{ scope.row.ischeck === 1 ? '通过' : '拒绝' }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="stock" label="总库存" align="center">

            </el-table-column>
            <el-table-column prop="min_price" label="价格" align="center"><template slot-scope="scope">
                    <span class="text-danger">￥{{ scope.row.min_price }}</span>
                    <span class="text-muted">￥{{ scope.row.min_oprice }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="150">
                <template slot-scope="scope">
                    <!-- slot-scope="scope"创建了一个名为scope的新作用域，
                             它包含了子组件传递给插槽的所有数据。然后我们可以使用v-for指令来遍历scope.items。 -->

                    <!-- plain镂空 -->
                    <el-button type="text" size="mini"
                        @click="navigate('shop-goods-create', scope.row.id)">基础设置</el-button>
                    <el-button type="text" size="mini" @click="navigate('shop-goods-sku', scope.row.id)"
                        :class="(scope.row.sku_type == 0 && !scope.row.sku_value) || (scope.row.sku_type == 1 && !scope.row.goods_skus.length) ? 'text-danger' : ''">商品规格</el-button>
                    <el-button type="text" size="mini" @click="navigate('shop-goods-attr', scope.row.id)"
                        :class="scope.row.goods_attrs.length === 0 ? 'text-danger' : ''">商品属性</el-button>
                    <el-button type="text" size="mini" @click="navigate('shop-goods-banner', scope.row.id)"
                        :class="scope.row.goods_banner.length === 0 ? 'text-danger' : ''">媒体设置</el-button>
                    <el-button type="text" size="mini" @click="navigate('shop-goods-content', scope.row.id)"
                        :class="!scope.row.content ? 'text-danger' : ''">商品详情</el-button>
                    <el-button type="text" size="mini">折扣设置</el-button>
                    <el-button type="text" size="mini" @click="delItem(scope.row)">删除商品</el-button>

                </template>
            </el-table-column>
        </el-table>
        <!-- 底部 -->
        <div style="height: 60px;">
            <el-footer class="border-top d-flex align-items-center px-0 position-fixed bg-white"
                style="bottom:0;left: 200px;right: 0; z-index: 100;">
                <!-- flex:1占剩余的所有空间 -->
                <div style="flex: 1;">
                    <el-pagination :current-change="page.current" :page-sizes="page.sizes" :page-size="page.size"
                        layout="total, sizes, prev, pager, next, jumper" :total="page.total"
                        @size-change="handleSizeChange" @current-change="handleCurrentChange">
                    </el-pagination>
                </div>

            </el-footer>
        </div>
    </div>
</template>
<script>
import buttonSearch from '@/components/common/button-search.vue';
import common from '@/common/mixins/common';


export default {
    name: "GoodsList",
    mixins: [common],
    inject: ['layout'],
    components: { buttonSearch },
    data() {
        return {
            preUrl: 'goods',
            tabIndex: 0,
            tabBars: [
                { name: "全部", key: 'all' },
                { name: "审核中", key: 'checking' },
                { name: "出售中", key: 'saling' },
                { name: "已下架", key: 'off' },
                { name: "库存预警", key: 'min_stock' },
                { name: "回收站", key: 'delete' },
            ],
            form: {
                title: '',

                category_id: '',
            },
            tableData: [],
            multipleSelection: [],
            cateOptions: []
        };
    },
    created() {

    },
    computed: {
        tab() {
            return this.tabBars[this.tabIndex].key
        },
        params() {
            let str = '';
            for (let key in this.form) {
                let val = this.form[key];
                if (val) {

                    str += `&${key}=${val}`;

                }
            }
            return str;
        }
    },
    filters: {
        tree(item) {
            if (item.level == 0) {
                return item.name
            }
            let str = ''
            for (let i = 0; i < item.level; i++) {
                str += i == 0 ? '|--' : '--'
            }
            return str + ' ' + item.name
        }
    },
    methods: {
        navigate(name, id) {
            this.$router.push({
                name: name,
                params: { id }
            })
        },

        getListResult(e) {
            console.log('!!!', e);
            this.tableData = e.list
            this.cateOptions = e.cates
        },


        // 清空筛选条件
        clearSearch() {
            this.form = {
                title: '',

                category_id: "",
            }
            // 
            // this.$refs.buttonSearch[this.tabIndex].closeSuperSearch();
            this.$refs.buttonSearch.closeSuperSearch();
        },
        // 搜索事件
        searchEvent(e = false) {
            // 简单搜索,如果e有值
            if (typeof e === 'string') {
                this.form.title = e
            }
            // 高级搜索事件
            this.getList()
        },
        doEvent(key) {
            this.layout.showLoading()
            this.$axios.post('/admin/goods/' + key, {
                ids: this.ids
            }, {
                token: true
            }).then(() => {
                this.layout.hideLoading()
                this.$message.success('恢复成功')
                this.getList()
            }).catch(() => {
                this.layout.hideLoading()
            })
        },
        // 修改商品或者上下架
        changeStatus(status) {
            if (this.ids.length === 0) {
                return this.$message({
                    type: 'error',
                    message: '请选择要操作的商品'
                })
            }
            this.layout.showLoading()
            this.$axios.post('/admin/goods/changestatus', {
                ids: this.ids,
                status
            }, {
                token: true
            }).then(() => {
                this.layout.hideLoading()
                this.getList()
            }).catch(() => {
                this.layout.hideLoading()

            })
        },
        // 搜索的实现
        getListUrl() {

            return `/admin/${this.preUrl}/${this.page.current}?limit=${this.page.size}&tab=${this.tab}${this.params}`;
        },
        checkGoods(item, ischeck) {
          
            this.layout.showLoading()
            this.$axios.post('/admin/goods/' + item.id + '/check', {
                ischeck
            }, {
                token: true
            }).then(() => {
                this.getList()
                this.$message.success('操纵成功')
                this.layout.hideLoading()
            }).catch(() => {
                this.layout.hideLoading()

            })
        }

    },
};
</script>
<style scoped>
.text {
    font-size: 14px;
}

.item {
    margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
    display: table;
    content: "";
}

.clearfix:after {
    clear: both
}
</style>